package org.com.yz.common.util;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.StringWriter;

/**
 * Created by IntelliJ IDEA.
 * User: tsaowe
 * Date: 11-9-1
 * Time: 上午9:13
 */
public class XmlFormatter {
    public static String formatXml(String str) throws Exception {
        Document document = null;
        document = DocumentHelper.parseText(str);
        // 格式化输出格式
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("gb2312");
        StringWriter writer = new StringWriter();
        // 格式化输出流
        XMLWriter xmlWriter = new XMLWriter(writer, format);
        // 将document写入到输出流
        xmlWriter.write(document);
        xmlWriter.close();

        return writer.toString();
    }

    public static String getLevelStr(int level) {
        StringBuffer levelStr = new StringBuffer();
        for (int levelI = 0; levelI < level; levelI++) {
            levelStr.append("\t");
        }
        return levelStr.toString();
    }

    public static String formatJson(String s) {
        //json 字符串
//        String s = "{\"code\":10000,\"msg\":null,\"data\":{\"id\":\"7aa0eb56-1026-4497-a42e-4c39f5e3dcf1\",\"topicId\":\"0876ab84-a478-417b-91bc-849843c191a5\",\"title\":null,\"commentId\":null,\"content\":\"" +
//                "开发者平台自动化测试：针对帖子发表评论" +
//                "\",\"images\":\"\",\"time\":\"2017-10-15 18:09:56\",\"userId\":\"61028f94-de92-4c65-aad3-2fc8614e1d34\",\"userName\":\"devautotest\",\"commentNum\":0,\"status\":0}}";
        int level = 0;
        //存放格式化的json字符串
        StringBuffer jsonForMatStr = new StringBuffer();
        for (int index = 0; index < s.length(); index++)//将字符串中的字符逐个按行输出
        {
            //获取s中的每个字符
            char c = s.charAt(index);
//          System.out.println(s.charAt(index));

            //level大于0并且jsonForMatStr中的最后一个字符为\n,jsonForMatStr加入\t
            if (level > 0 && '\n' == jsonForMatStr.charAt(jsonForMatStr.length() - 1)) {
                jsonForMatStr.append(getLevelStr(level));
//                System.out.println("123"+jsonForMatStr);
            }
            //遇到"{"和"["要增加空格和换行，遇到"}"和"]"要减少空格，以对应，遇到","要换行
            switch (c) {
                case '{':
                case '[':
                    jsonForMatStr.append(c + "\n");
                    level++;
                    break;
                case ',':
                    jsonForMatStr.append(c + "\n");
                    break;
                case '}':
                case ']':
                    jsonForMatStr.append("\n");
                    level--;
                    jsonForMatStr.append(getLevelStr(level));
                    jsonForMatStr.append(c);
                    break;
                default:
                    jsonForMatStr.append(c);
                    break;
            }
        }
        return  jsonForMatStr.toString();
    }

    public static void main(String[] args) throws Exception {
        String s = "<FOX><SIGNONMSGSRQV1><SONRQ><DTCLIENT>2018-09-13 18:46:52</DTCLIENT><CID>3500967023</CID><USERID>mdkj2016</USERID><USERPASS>yeshuo20160712</USERPASS><GENUSERKEY>N</GENUSERKEY></SONRQ></SIGNONMSGSRQV1><SECURITIES_MSGSRQV1><XFERTRNRQ><TRNUID>7f9a59b5696f47b88217d04fb9c5fb</TRNUID><XFERRQ><XFERINFO><ACCTFROM><ACCTID>356940100100178952</ACCTID></ACCTFROM><ACCTTO INTERBANK=\"N\" LOCAL=\"Y\"><ACCTID>6222031202000364595</ACCTID><NAME>李</NAME><BANKDESC>中国工商银行</BANKDESC><BANKNUM>102100099996</BANKNUM><CITY>浙江省 杭州市 西湖区</CITY></ACCTTO><TRNAMT>0.01</TRNAMT><PURPOSE>摇钱吧提现</PURPOSE><MEMO>摇钱吧提现</MEMO></XFERINFO></XFERRQ></XFERTRNRQ></SECURITIES_MSGSRQV1></FOX>";
        System.out.println(formatXml(s));
    }
}
